$(document)
		.ready(
				function() {
					var contextPath = document.location.pathname;
					var index = contextPath.substr(1).indexOf("/");
					contextPath = contextPath.substr(1, index);
					var basePath = "/" + contextPath;
					var totalCount = $("#totalCount").val();
					var pSize = $("#pageSize").val();
<<<<<<< HEAD
					var currentPage=1
=======
					
>>>>>>> refs/remotes/origin/master
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					if (pageCount == null || pageCount == ""
							|| pageCount == "undefined") {
						pageCount = 1;
<<<<<<< HEAD
						currentPage=1;
=======
>>>>>>> refs/remotes/origin/master

					}
					if (totalCount == null || totalCount == ""
						|| totalCount == "undefined") {
						totalCount = 0;

				}
					$('form')
							.submit(
									function() {
										if (typeof jQuery.data(this,
												"disabledOnSubmit") == 'undefined') {
											jQuery.data(this,
													"disabledOnSubmit", {
														submited : true
													});
											$(
													'input[type=submit], input[type=button]',
													this).each(
													function() {
														$(this).attr(
																"disabled",
																"disabled");
													});
											return true;
										} else {
											return false;
										}
									});

					$("#optBtn").delegate(
							'#saveBtn',
							'click',
							function() {

								$("#productDetail").attr("method", "post");

								var proId = $(
										"#productDetail input[id='proId']")
										.val();
								// alert(proId);
								if (proId == "" || proId == null
										|| proId == "undefined") {

									$("#productDetail").attr("action",
											"save.do");
									alert("save");
								} else {
									$("#productDetail").attr("action",
											"update.do");
									alert("update");
								}
								$("#productDetail").submit();

							});
				
					$('#pagination2')
							.jqPaginator(
									{
//										totalPages : pageCount,
//										visiblePages : pageCount,
										totalCounts:totalCount,
										pageSize : pSize,
										visiblePages : pageCount,
										currentPage:1,

										prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											// alert("pageIndex="+num);
											// alert("type="+type);
											$("#pageIndex").val(num);
											var typeId = $("#proTypeId").val();
											// alert("type----Id="+typeId);
											var queryUrl = "queryByPage.do";
											$
													.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex : num,
															pageSize : pSize,
															typeId : typeId
														},
														cache : false,
														success : function(data) {

															$(
																	"#products tr:not(:first)")
																	.remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var product = results[i];

																var rowContent = "<tr>"
																		+ "<td><input name='proId' class='checkbox' type='checkbox' value='"
																		+ product.id
																		+ "'/></td>"
																		+ "<td>"
																		+ product.id
																		+ "</td>"
																		+ "<td>"
																		+ product.proCode
																		+ "</td>"
																		+ "<td>"
																		+ product.proName
																		+ "</td>"
																		+ "<td>"
																		+ product.typeName
																		+ "</td>"
																		+ "<td>"
																		+ product.grown
																		+ "</td>"
																		+ "<td>"
																		+ product.total
																		+ "</td>"
																		+ "<td>"
																		+ product.enterPrice
																		+ "</td>"
																		+ "<td>"
																		+ product.salePrice
																		+ "</td>"
																rowContent = rowContent
																		+ "<td><button name='editBtn' objId='"
																		+ product.id
																		+ "' class='btn btn-sm-block'>修改</button></td>"
																		+ "<td><button name='viewRule' objId='"
																		+ product.id
																		+ "'"
																		+ "class='btn btn-sm-block'>查看规格库</button></td>"
																		+ "<td><button name='viewResource' objId='"
																		+ product.id
																		+ "'"
																		+ "class='btn btn-sm-block'>查看资源库</button></td>"
																		+ +"</tr>";
																$("#products")
																		.append(
																				rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});
					

					/**
					 * 全选当前页记录
					 */
					$("#products").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='proId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#products").delegate('[name="editBtn"]', 'click',
							function() {
								var proId = $(this).attr("objId");

								var editUrl = "show.do";
								$.ajax({

									url : editUrl,
									dataType : 'json',
									data : {
										id : proId

									},
									cache : false,

									success : function(data) {

										loadProduct(data);
									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										refresh();
									}
								});

							});

					$("#products").delegate('[name="viewResource"]', 'click',
							function() {
								var proId = $(this).attr("objId");
<<<<<<< HEAD
							
=======
								alert("////////proId=" + proId);
>>>>>>> refs/remotes/origin/master

								loadResource(proId, 0);
							});

					$("#products").delegate(
							'[name="viewRule"]',
							'click',
							function() {
								var proId = $(this).attr("objId");

								var editUrl = basePath
										+ "/manage/product/proDetails/list.do";
								document.location.href = editUrl + "?id="
										+ proId;
							});

					$("#tools").delegate('#deleteBtn', 'click', function() {
						delChk = $('input[name="proId"]:checked');

						if (delChk.length > 0) {
							if (confirm("Are you sure delete the record?")) {

								var ids = "";
								/* var delUrl = "delete.do?ids="; */
								var delUrl = "delete.do";
								var idArr = new Array();

								for (var i = 0; i < delChk.length; i++) {
									idArr.push(delChk.get(i).value);

								}

								$.ajax({
									url : delUrl,
									type : "POST",
									data : {
										ids : idArr.toString()
									},
									cache : false,

									success : function(html) {
										alert("Delete successfully");
										refresh();
									},
									error : function(html) {
										alert("Delete failly");
										refresh();
									}
								});
							} else {
								return;
							}
						} else {
							alert("Please select the record to delete");
							return;
						}
					});

					
					$("#typeId").delegate($(this),'change',function(){
						
						var typeId=$(this).val();
						
						queryUrl=encodeURI("queryPrefixCodeByType.do?typeId="+typeId+"");
						if(typeId!="" && typeId>0)
							{
							$.ajax({
								url : queryUrl,
								dataType : 'text',
								
								cache : false,

								success : function(data) {
									
									$("#proCode").val(data)
								},
								error : function(data) {
									alert("获取产品编号出错");
								}
							});
							}else{
								alert("请选择产品类型");
							}
					});
					
					
					
					$("#tools").delegate('#addProduct', 'click', function() {

						var typeId = $("#proTypeId").val();
						// alert("******proId******"+typeId);
						var editUrl = "add.do";
						$.ajax({

							url : editUrl,
							dataType : 'json',
							data : {
								id : typeId
							},
							cache : false,

							success : function(data) {

								// alert("************"+data.typeId);
								$("#productDialog input").each(function() {
									$(this).prop("value", null);
									$(this).prop("readonly", false);

								});
								$("#productDialog textarea").each(function() {
									$(this).prop("value", null);

								});

								loadProduct(data)

							},
							error : function(html) {
								alert(html);
								alert("Load Data failly");
								refresh();
							}
						});

					});
<<<<<<< HEAD
					
					
					
					

					function loadResource(proId, type) {
			
						$("#resourceDialog input[id='proId']").val(proId);
						var idx = 0;
						$("#baseTab").addClass("active");
						$("#pictrues").addClass("active");
						$("#itemsTab").removeClass("active");
						$("#videos").removeClass("active");
						$("#resourceDialog").modal('show');
						
=======

					function loadResource(proId, type) {
						var editUrl = "showResouce.do";

						$
								.ajax({

									url : editUrl,
									dataType : 'json',
									data : {
										id : proId,
										pageSize : 8,
										pageIndex : 1,
										resType : type

									},
									cache : false,

									success : function(data) {
										$("#resourceDialog").modal('show');
										$("#resourceDialog input[id='proId']")
												.val(data.proId);
										var  idx = 0;
										$("#baseTab").addClass("active");
										$("#pictrues").addClass("active");
										$("#itemsTab").removeClass("active");
										$("#videos").removeClass("active");
										// initResImgPagination(8,1);
										var results = data.resultList;
										var rowLen=0;
										if(results.length%4==0){
											rowLen=results.length/4;
										}else{
											rowLen=parseInt(results.length/4)+1;
										}
										if (type == 0) {

											for (k = 0; k < rowLen; k++) {
												alert("k="+k);
												var rowContent = "<tr id='"+k+"'>";
												for (i = 0; i < 4; i++) {
													
													if(idx<results.length){
												    imgRes = results[idx];
													
													rowContent = rowContent
															+ "<td style='text-align: center;'>";
													rowContent = rowContent
															+ "<dl><img id='"
															+ idx
															+ "' alt='' src='"
															+ imgRes.url
															+ "' width='100' height='100' /></dl><dl style='text-align: center;'>"
															+ imgRes.resName
															+ "</dl></td>";
													idx++;
													}
													
												}
												rowContent = rowContent+ "</tr>";
												
												$("#imageTable").append(rowContent);

											}
										

										} else if (type == 1) {

											for (j = 0; j < results.length; j++) {

												var res = results[j];
												alert(res.resName);
												var rowContent = "<tr><td style='text-align: center;'>";
												rowContent = rowContent
														+ "<dl><img id='res_001' alt='' src='<%=request.getContextPath()%>/HRTWEB/images/23/201501161542325133.jpg'";
												rowContent = rowContent
														+ " width='100' height='100' /></dl><dl style='text-align: center;'>图片1</dl>";
												rowContent = rowContent
														+ "</td><td style='text-align: center;'><dl><img id='res_003' alt='' src='<%=request.getContextPath()%>/HRTWEB/images/23/201501161542325133.jpg'";
												rowContent = rowContent
														+ "	width='100' height='100' /></dl><dl style='text-align: center;'>图片1</dl></td><td style='text-align: center;''>";
												rowContent = rowContent
														+ "<dl><img id='res_003' alt='' src='<%=request.getContextPath()%>/HRTWEB/images/23/201501161542325133.jpg' width='100' height='100' />";
												rowContent = rowContent
														+ "</dl><dl style='text-align: center;'>图片1</dl></td><td style='text-align: center;'><dl><img id='res_004' alt='' src='<%=request.getContextPath()%>/HRTWEB/images/23/201501161542325133.jpg'";
												rowContent = rowContent
														+ "width='100' height='100' /></dl><dl style='text-align: center;'>图片1</dl></td></tr>"

												$("#videoTable").append(
														rowContent);
											}
										}

									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										refresh();
									}
								});
>>>>>>> refs/remotes/origin/master

					}
					
					

					function loadProduct(product) {

						
						$("#proId").val(product.id);
						$("#proCode").val(product.proCode);
						$("#proName").val(product.proName);
						$("#grown").val(product.grown);
						$("#factory").val(product.factory);
						$("#typeId").val(product.typeId);
						$("#unit").val(product.unit);
						$("#isdemo").val(product.isdemo);
						
						$("#enterPrice").val(product.enterPrice);
						$("#salePrice").val(product.salePrice);
						$("#description").val(product.description);
						$("#productDialog").modal("show");
					}

					function vaildate(form) {
						return true;
					}

					function refresh() {
						window.location.reload();
					}
				});
